package fag.editor;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.event.EventListenerList;

import fag.core.model.Animation;
import fag.core.model.Model;

public class AnimationModifier extends JPanel implements ActionListener {

	private static final long serialVersionUID = -8279167156393580322L;
	
	private final static int DEFAULT_WIDTH = 350;
	private final static int DEFAULT_HEIGHT = 80;
	
	private JButton btAddAnimation = new JButton(new ImageIcon(getClass().getResource("/imgs/+.png"),"PlusIcon"));
	private JButton btRemoveAnimation = new JButton(new ImageIcon(getClass().getResource("/imgs/-.png"),"MoinsIcon"));
	private JButton btPlayAnimation = new JButton(new ImageIcon(getClass().getResource("/imgs/play.png"),"PlayIcon"));
	private JButton btRenameAnimation = new JButton(new ImageIcon(getClass().getResource("/imgs/rename.png"),"RenameIcon"));
	private JComboBox cbAnimation = new JComboBox();
	
	protected final static String ACTION_ADD_ANIMATION = "Add Animation";
	protected final static String ACTION_REMOVE_ANIMATION = "Remove Animation";
	protected final static String ACTION_PLAY_ANIMATION = "Play Animation";
	protected final static String ACTION_RENAME_ANIMATION = "Rename Animation";
	protected final static String ACTION_SELECTION_ANIMATION_CHANGED = "Selection Animation Changed";
	
	private final EventListenerList listeners = new EventListenerList();
	
	private Model model = null;
	private int selectedAnimationIndex = 0;
	
	
	public AnimationModifier() {
		
		setBorder(BorderFactory.createTitledBorder("Animation"));
		
		add(btAddAnimation);
		add(btRemoveAnimation);
		add(btPlayAnimation);
		add(btRenameAnimation);
		add(cbAnimation);
		
		btAddAnimation.setPreferredSize(new Dimension(40,40));
		btRemoveAnimation.setPreferredSize(new Dimension(40,40));
		btPlayAnimation.setPreferredSize(new Dimension(40,40));
		btRenameAnimation.setPreferredSize(new Dimension(40,40));
		cbAnimation.setPreferredSize(new Dimension(100,40));
		
		btAddAnimation.addActionListener(this);
		btRemoveAnimation.addActionListener(this);
		btPlayAnimation.addActionListener(this);
		btRenameAnimation.addActionListener(this);
		cbAnimation.addActionListener(this);
		
		repaint();
	}
	
	public int getCurrentAnimationIndex() {
		
		return selectedAnimationIndex;
	}
	
	@Override
	public Dimension preferredSize() {
		
		return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}
	
	public void addActionListener(ActionListener listener) {
		
		listeners.add(ActionListener.class,listener);
	}
	
	public void removeActionListener(ActionListener listener) {
		
		listeners.remove(ActionListener.class, listener);
	}
	
	public void setModel(Model m) {
		
		model = m;
		selectedAnimationIndex = 0;
		cbAnimation.removeAllItems();
		
		for(Animation a : model.getListAnimation()) {
			
			cbAnimation.addItem(a.getName());
		}
		cbAnimation.updateUI();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource() == btAddAnimation)
			addAnimation();
		
		if(e.getSource() == btRemoveAnimation)
			removeAnimation();
		
		if(e.getSource() == btPlayAnimation)
			playAnimation();
		
		if(e.getSource() == btRenameAnimation)
			renameAnimation();
		
		if(e.getSource() == cbAnimation) {
			
			int old = selectedAnimationIndex;
			selectedAnimationIndex = cbAnimation.getSelectedIndex();
			
			if(old != selectedAnimationIndex && selectedAnimationIndex != -1)
				selectedAnimationChanged();
		}
	}

	protected void removeAnimation() {
		
		for(ActionListener listener : listeners.getListeners(ActionListener.class)) 
			listener.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_LAST + 1,ACTION_REMOVE_ANIMATION));
	}
	
	protected void renameAnimation() {
		
		for(ActionListener listener : listeners.getListeners(ActionListener.class)) 
			listener.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_LAST + 1,ACTION_RENAME_ANIMATION));
	}

	protected void playAnimation() {
	
		for(ActionListener listener : listeners.getListeners(ActionListener.class)) 
			listener.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_LAST + 1,ACTION_PLAY_ANIMATION));
	}

	protected void addAnimation() {
	
		for(ActionListener listener : listeners.getListeners(ActionListener.class)) 
			listener.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_LAST + 1,ACTION_ADD_ANIMATION));
	}
	
	protected void selectedAnimationChanged() {
		
		for(ActionListener listener : listeners.getListeners(ActionListener.class)) 
			listener.actionPerformed(new ActionEvent(this,ActionEvent.ACTION_LAST + 1,ACTION_SELECTION_ANIMATION_CHANGED));
	}
}
